﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Common;
using CNCSoft.EData;

namespace CNCSoft.ErpApps.TMS.longGiaoVu
{
    public partial class frmHocBoTro : Form
    {
        SqlConnection conn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
        DataSet ds = new DataSet();
        string _madonvi = GlobalVariable.WSInfo.CompanyID;
        string _makhoahoc;
        bool _insert = false;
        bool _edit = false;

        public frmHocBoTro()
        {
            InitializeComponent();
        }

        private void frmHocBoTro_Load(object sender, EventArgs e)
        {
            LoadControl();
            LoadButton();
        }

        private void searchLookUpEdit_KhoaHocDangDoi_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                _makhoahoc = searchLookUpEdit_KhoaHocDangDoi.EditValue.ToString();
                LoadInvoiceKhoaHocByKhoaHoc(_makhoahoc);
                searchLookUpEdit_HocVienLop.Properties.DataSource = ds.Tables["TableInvoiceKhoaHocByKhoaHoc"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            _insert = true;
            _edit = false;
            LoadButton();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (_insert)
            {
                InsertHocBoTro(searchLookUpEdit_HocVienLop.EditValue.ToString(), searchLookUpEdit_KhoaHoc.EditValue.ToString(),
                                searchLookUpEdit_KhoaHocDangDoi.EditValue.ToString(), Convert.ToDateTime(dateEdit_NgayHocBoTro.EditValue), "Bổ trợ");
                LoadHocBoTro();
                gridControl_Data.DataSource = ds.Tables["TableHocVienBoTro"];
            }
            else if (_edit)
            {
                UpdateData();
                LoadHocBoTro();
                gridControl_Data.DataSource = ds.Tables["TableHocVienBoTro"];
            }
            //-----------------------
            _insert = false;
            _edit = false;
            LoadButton();
        }

        private void btnChinhSua_Click(object sender, EventArgs e)
        {
            _insert = false;
            _edit = true;
            LoadButton();
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            _insert = false;
            _edit = false;
            LoadButton();
        }

        #region "Coding"
        private void LoadControl()
        {
            try
            {
                LoadKhoaHocWaiting();
                searchLookUpEdit_KhoaHocDangDoi.Properties.DataSource = ds.Tables["TableKhoaHocWaiting"];
                //-----------------------
                LoadKhoaHoc();
                searchLookUpEdit_KhoaHoc.Properties.DataSource = ds.Tables["TableKhoaHoc"];
                //-----------------------------
                dateEdit_NgayHocBoTro.EditValue = DateTime.Now.ToShortDateString();
                LoadHocBoTro();
                gridControl_Data.DataSource = ds.Tables["TableHocVienBoTro"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadKhoaHoc()
        {
            if (ds.Tables["TableKhoaHoc"] != null)
                ds.Tables["TableKhoaHoc"].Clear();
            string sql = @"usp_AllKhoaHocByDonVi @madonvi = '" + _madonvi + "'";
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            da.Fill(ds, "TableKhoaHoc");
        }

        private void LoadKhoaHocWaiting()
        {
            try
            {
                if (ds.Tables["TableKhoaHocWaiting"] != null)
                    ds.Tables["TableKhoaHocWaiting"].Clear();
                string sql = @"usp_AllKhoaHocWaiting @madonvi = '" + _madonvi + "'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableKhoaHocWaiting");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadInvoiceKhoaHocByKhoaHoc(string makhoahoc)
        {
            try
            {
                if (ds.Tables["TableInvoiceKhoaHocByKhoaHoc"] != null)
                    ds.Tables["TableInvoiceKhoaHocByKhoaHoc"].Clear();
                string sql = @"usp_AllInvoiceKhoaHocByDonViAndKhoaHoc @madonvi = '" + _madonvi + @"', @makhoahoc = '" + makhoahoc + "'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableInvoiceKhoaHocByKhoaHoc");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadHocBoTro()
        {
            try
            {
                if (ds.Tables["TableHocVienBoTro"] != null)
                    ds.Tables["TableHocVienBoTro"].Clear();
                string sql = @"usp_AllHocVienBoTro";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableHocVienBoTro");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //----------Insert into HocBoTro------------------------------

        private void InsertHocBoTro(string invoicenumber, string makhoahocbotro, string makhoahocsehoc, DateTime ngayhocbotro, string tinhtrang)
        {
            try
            {
                string sql = @"usp_InsertHocBoTro @invoicenumber = @inv, @makhoahocbotro = @khbt, 
                                @makhoahocsehoc = @khsh, @ngayhocbotro = @ngay, @tinhtrang = @status";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@inv", invoicenumber);
                cmd.Parameters.AddWithValue("@khbt", makhoahocbotro);
                cmd.Parameters.AddWithValue("@khsh", makhoahocsehoc);
                cmd.Parameters.AddWithValue("@ngay", ngayhocbotro);
                cmd.Parameters.AddWithValue("@status", tinhtrang);
                int i;
                try
                {
                    conn.Open();
                    i = cmd.ExecuteNonQuery();   
                }
                finally
                {
                    if (conn != null)
                        conn.Close();
                }
                if (i > 0)
                {
                    MessageBox.Show("Thêm thành công!!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //----------Update tinh trang hoc bo tro----------------------

        private void UpdateData()
        {
            try
            {
                string sql = @"select * from HocBoTro";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                DataHelper.buildDataCommands(ref da);
                da.Update(ds.Tables["TableHocVienBoTro"]);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //-----------Load button--------------------------------------

        private void LoadButton()
        {
            if(_insert == false && _edit == false)
            {
                btnThem.Enabled = true;
                btnBoQua.Enabled = true;
                btnLuu.Enabled = false;
                btnChinhSua.Enabled = true;
            }
            else if (_insert)
            {
                btnThem.Enabled = false;
                btnBoQua.Enabled = true;
                btnLuu.Enabled = true;
                btnChinhSua.Enabled = true;
            }
            else if (_edit)
            {
                btnThem.Enabled = true;
                btnBoQua.Enabled = true;
                btnLuu.Enabled = true;
                btnChinhSua.Enabled = false;
            }
        }
        #endregion
    }
}
